﻿using Dapper;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XUnitTestEnter
{
    public class GenEntity
    {
        public static string GenTable(string table,string db= "yiyandb") 
        {
            // https://mysqlconnector.net/
            using (var connection = new MySqlConnection("Server=127.0.0.1;User ID=root;Password=123456;Database=information_schema"))
            {
                connection.Open();
                var columns = connection.Query("select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from `columns` where table_name = @Table AND TABLE_SCHEMA  = @Db ORDER BY ORDINAL_POSITION;", new { Table = table, Db = db }).ToList();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("    public class " + table);
                sb.AppendLine("    {");
                foreach (var c in columns) 
                {
                    
                    sb.AppendLine(string.Format(@"        /// <summary>
        /// {0}
        /// </summary>",c.COLUMN_COMMENT));
                    var data_type = "string";
                    switch (c.DATA_TYPE) {
                        case "bigint":
                            data_type = "long";
                            break;
                        case "datetime":
                            data_type = "DateTime";
                            break;
                        case "decimal":
                            data_type = "decimal";
                            break;
                        case "int":
                            data_type = "int";
                            break;
                        case "tinyint":
                            data_type = "bool";
                            break;
                        case "bit":
                            data_type = "bool";
                            break;
                        default: 
                            data_type = "string";
                            break;
                    }
                    sb.AppendLine(string.Format("        public {0} {1} {{ get; set; }}", data_type, c.COLUMN_NAME));
                }
                sb.AppendLine("    }");
                return sb.ToString();
            }
        }
        
    }
}
